package cn.tedu.straw.portal.controller;


import cn.tedu.straw.portal.dto.PostCommentDTO;
import cn.tedu.straw.portal.ex.ParamValidationException;
import cn.tedu.straw.portal.security.LoginUserInfo;
import cn.tedu.straw.portal.service.ICommentService;
import cn.tedu.straw.portal.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tedu.cn
 * @since 2020-11-12
 */
@RestController
@RequestMapping("/portal/comments")
public class CommentController {
    @Autowired
    private ICommentService commentService;

    @RequestMapping("/post")
    public Result<Void> postComment(@Valid PostCommentDTO postCommentDTO,
                                    @AuthenticationPrincipal LoginUserInfo loginUserInfo,
                                    BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            String errMessage= bindingResult.getFieldError().getDefaultMessage();
            throw new ParamValidationException(errMessage);
        }
        commentService.post(postCommentDTO,loginUserInfo.getId(),loginUserInfo.getNickName());
        return Result.ok();
    }
}
